package com.andy.blog.admin.controller;

import com.andy.blog.admin.dto.MessageDTO;
import com.andy.blog.admin.service.MessageBusService;
import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.annotation.RepeatSubmit;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.dal.vo.MessageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by andy on 2023/8/5 07:51
 */
@Api(tags = "留言板")
@RestController
public class MessageController {

    @Resource
    private MessageBusService messageBusService;


    @GetMapping("/portal/message/listMessage")
    public GeneralResponseResult<List<MessageDTO>> listMessage(MessageVO messageVO) {
        return GeneralResponseResult.success(messageBusService.listMessage(messageVO));
    }

    /**
     * 新增留言
     */
    //@PreAuthorize("@ss.hasPermi('system:sensitive:add')")
    @ApiOperation(value = "新增留言")
    @Log(title = "留言管理", businessType = BusinessTypeEnum.INSERT)
    @PostMapping("/portal/message/addMessage")
    @RepeatSubmit(interval = 3000, message = "亲爱的，您留言太快了，慢点~")
    public GeneralResponseResult<Void> add(@Validated @RequestBody MessageVO messageVO) {
        messageBusService.addMessage(messageVO);
        return GeneralResponseResult.success();
    }

    /**
     * 获取分页数据
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/message/listPage")
    public GeneralResponseResult<PageUtilsDTO<MessageDTO>> list(MessageVO messageVO) {
        return GeneralResponseResult.success(messageBusService.selectPage(messageVO));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "删除留言", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/message/{id}")
    public GeneralResponseResult<Void> remove(@PathVariable Long id) {
        messageBusService.deleteById(id);
        return GeneralResponseResult.success();
    }
}
